﻿@page
@model DragSort.IndexModel
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf

<html>
<head runat="server">
    <title>DragSort Example</title>
    <meta charset="utf-8" />
    <style type="text/css">
        body { font-family:Arial; font-size:12pt; padding:20px; width: 800px; margin:20px auto; border:solid 1px black; }
        h1 { font-size:16pt; }
        h2 { font-size:13pt; }
        ul { width:350px; list-style-type: none; margin:0px; padding:0px; }
        li { float:left; padding:5px; width:100px; height:100px; }
        li div { width:90px; height:50px; border:solid 1px black; background-color:#E0E0E0; text-align:center; padding-top:40px; }
        .placeHolder div { background-color:white !important; border:dashed 1px gray !important; }
    </style>
</head>
<body>
    <div>
        <h1>jQuery List DragSort ASP.NET Example</h1>
        
        <a href="http://dragsort.codeplex.com/">Homepage</a><br/>
        <br/>
        
        <h2>Save list order with ajax:</h2>
        
        <ul id="gallery">
            @for (var i = 0; i<Model.Data.Length; i++)
            {
                <li data-itemid='@i'>
                    <div>@Model.Data[0]</div>
                </li>
            }
        </ul>
        
        <script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.slim.min.js"></script>
        <script type="text/javascript" src="jquery.dragsort.js"></script>
        <script type="text/javascript">
            $("#gallery").dragsort({ dragSelector: "div", dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });
            
            function saveOrder() {
                var data = $("#gallery li").map(function() { return $(this).data("itemid"); }).get();
                fetch("?handler=SaveOrder", {
                    method: "POST",
                    headers: {
                        'content-type': 'application/json; charset=utf-8',
                        'RequestVerificationToken': '@Xsrf.GetAndStoreTokens(HttpContext).RequestToken'
                    },
                    body: JSON.stringify(data)
                });
            };
        </script>
        
        <div style="clear:both;"></div>
    </div>
</body>
</html>
